package cn.com.gome.sn.inapi.base;

import cn.com.gome.common.security.md5.MD5Util;
import cn.com.gome.frame.util.StringUtils;
import net.sf.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2/22/16.
 */
public class AdvanceApiBaseController {

    /**
     * 处理成功
     */
    public static final String RESULT_SUCCESS="0000";
    /**
     * 重复请求
     */
    public static final String RESULT_REPEAT="0001";
    /**
     * 处理失败
     */
    public static final String RESULT_FAIL="0002";

    /**
     * 对请求参数进行初始校验
     * @param paramMap
     * @return
     */
    public boolean checkParamMap(Map<String, Object> paramMap){
        String reqTime      = (String)paramMap.get("reqTime");//请求时间
        String version      = (String)paramMap.get("version");//版本号
        String reqMessageId = (String)paramMap.get("reqMessageId");//请求ID
        String systemNo     = (String)paramMap.get("systemNo");//系统编号
        String encReqInfo   = (String)paramMap.get("encReqInfo");//加密请求信息
        String reqType      = (String)paramMap.get("reqType");//交易类型
        String sign         = (String)paramMap.get("sign");//签名信息
        if (
                StringUtils.isEmpty(reqTime)
                        || StringUtils.isEmpty(version)
                        || StringUtils.isEmpty(reqMessageId)
                        || StringUtils.isEmpty(systemNo)
                        || StringUtils.isEmpty(encReqInfo)
                        || StringUtils.isEmpty(reqType)
                        || StringUtils.isEmpty(sign)
                ){
            return false;
        }
        return true;
    }

    /**
     * 对请求参数进行初始校验，无加密五签名验证
     * @param paramMap
     * @return
     */
    public boolean checkParamMapne(Map<String, Object> paramMap){
        String reqTime      = (String)paramMap.get("reqTime");//请求时间
        String version      = (String)paramMap.get("version");//版本号
        String reqMessageId = (String)paramMap.get("reqMessageId");//请求ID
        String systemNo     = (String)paramMap.get("systemNo");//系统编号
        String reqType      = (String)paramMap.get("reqType");//交易类型
        if (
                StringUtils.isEmpty(reqTime)
                        || StringUtils.isEmpty(version)
                        || StringUtils.isEmpty(reqMessageId)
                        || StringUtils.isEmpty(systemNo)
                        || StringUtils.isEmpty(reqType)
                ){
            return false;
        }
        return true;
    }

    /**
     * 进行md5验证签名
     *
     * @param systemNo
     * @param reqTime
     * @param encReqInfo
     * @param sign
     * @return
     */
    public boolean validateMd5(String systemNo, String reqTime, String encReqInfo, String sign, String md5Key) {
        boolean ret = false;
        String formd5Str = systemNo + reqTime + encReqInfo + md5Key;
        String mysign = null;
        try {
            mysign = MD5Util.encodeMessage(formd5Str.getBytes("utf-8"));
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        if (mysign.equalsIgnoreCase(sign)) {//忽略大小写的签名
            ret = true;
        }
        return ret;
    }

    /**
     * 响应报文处理
     *
     * @param returnMsg    业务逻辑处理结果
     * @param reqMessageId 请求ID
     * @return
     */
    public String returnMsg(Map<String, Object> returnMsg, String reqMessageId) {
        Map<String, Object> map = null;
        if (returnMsg == null) {
            map = new HashMap<String, Object>();
        } else {
            map = returnMsg;
        }
        map.put("reqMessageId", reqMessageId);

        SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        map.put("serverTime", simple.format(new Date()));
        JSONObject resultJsonObj = JSONObject.fromObject(map);
        String returnStr = resultJsonObj.toString();
        return returnStr;
    }

    /**
     * 响应报文处理
     * @param returnMsg    业务逻辑处理结果
     * @param reqMessageId 请求ID
     * @return
    */
    public String returnMsg(String resCode,String resDesc,Map<String, Object> returnMsg, String reqMessageId) {
        Map<String, Object> map = null;
        if (returnMsg == null) {
            map = new HashMap<String, Object>();
        } else {
            map = returnMsg;
        }
        map.put("resCode",resCode);
        map.put("resDesc",resDesc);
        map.put("reqMessageId", reqMessageId);

        SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        map.put("serverTime", simple.format(new Date()));
        JSONObject resultJsonObj = JSONObject.fromObject(map);
        String returnStr = resultJsonObj.toString();
        return returnStr;
    }


}
